package com.alipay.sdk.util.cloudsaleapi;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.sdk.util.DateUtil;
import com.alipay.sdk.util.LogUtil;
import com.alipay.sdk.util.cloudsaleapi.bean.QuerySignBizContentReq;
import com.alipay.sdk.util.cloudsaleapi.bean.WithholdReq;
import com.alipay.sdk.util.cloudsaleapi.enumc.IotPayAction;
import com.alipay.sdk.util.cloudsaleapi.enumc.ProductCode;
import com.alipay.sdk.util.cloudsaleapi.enumc.Scene;

import java.util.Random;

/**
 * @ProjectName: 团餐
 * @ClassName: CloudSaleApi
 * @Description: java类作用描述
 * @Author: bd-001935
 * @CreateDate: 2022/8/23 2:10 下午
 */
public class CloudSaleApiUtil {

    public static JSONArray getGoodDetail(String amount, JSONArray goods) {
        if (goods != null && !goods.isEmpty()) {
            return goods;
        }
        JSONArray array = new JSONArray();
        JSONObject goodJson = new JSONObject();
        goodJson.put("goods_id", "default");
        goodJson.put("goods_name", "默认商品");
        goodJson.put("quantity", 1);
        goodJson.put("price", amount);
        array.add(goodJson);
        return array;
    }

    private static String getOutTradeNoRandom() {
        StringBuilder random = new StringBuilder();
        random.append(DateUtil.getCurrentDateTime());
        for (int i = 0; i < 3; i++) {
            random.append(new Random().nextInt(10));
        }
        return random.toString();
    }

    /**
     * 商户代扣 bizContent
     *
     * @param amount         金额
     * @param uid            用户uid
     * @param deviceNo       设备编号
     * @param authCode       authCode
     * @param terminalParams 交易加签
     * @param isvPid         isvPid
     * @param subject        主题
     * @param isFace         刷脸/扫码
     * @return
     */
    public static WithholdReq.BizContent getWithholdBizContent(String amount, String uid,
                                                               String deviceNo, String authCode,
                                                               String terminalParams, String isvPid,
                                                               String subject, String senceNo, boolean isFace) {
        return getWithholdBizContent(amount, uid, deviceNo, authCode,
                terminalParams, isvPid, subject, null, senceNo, isFace);
    }

    /**
     * 当面付 bizContent
     *
     * @return
     */
    public static WithholdReq.BizContent getFacePayBizContent(String amount, String uid,
                                                              String deviceNo, String authCode,
                                                              String terminalParams, String isvPid,
                                                              String subject, boolean isFace) {
        return getFacePayBizContent(amount, uid, deviceNo, authCode,
                terminalParams, isvPid, subject, null, isFace);
    }

    public static WithholdReq.BizContent getWithholdBizContent(String amount, String uid,
                                                               String deviceNo, String authCode,
                                                               String terminalParams, String isvPid,
                                                               String subject, JSONArray goods,
                                                               String senceNo, boolean isFace) {
        return getBizContent(amount, uid, deviceNo, authCode,
                terminalParams, isvPid, subject, goods, senceNo, isFace, true);
    }

    public static WithholdReq.BizContent getFacePayBizContent(String amount, String uid,
                                                              String deviceNo, String authCode,
                                                              String terminalParams, String isvPid,
                                                              String subject, JSONArray goods,
                                                              boolean isFace) {
        return getBizContent(amount, uid, deviceNo, authCode,
                terminalParams, isvPid, subject, goods, null, isFace, false);
    }

    /**
     * @param amount         金额
     * @param uid            用户uid
     * @param deviceNo       设备编号
     * @param authCode       authCode
     * @param terminalParams 交易加签
     * @param isvPid         isvPid
     * @param subject        主题
     * @param isFace         刷脸/扫码
     * @param isWithhold     商户代扣/当面付
     * @return
     */
    private static WithholdReq.BizContent getBizContent(String amount, String uid,
                                                        String deviceNo, String authCode,
                                                        String terminalParams, String isvPid,
                                                        String subject, JSONArray goods,
                                                        String senceNo,
                                                        boolean isFace, boolean isWithhold) {
        WithholdReq.BizContent bizContent = new WithholdReq.BizContent();
        bizContent.total_amount = amount;
        //商家交易号,需保证在商户应用下唯一
        bizContent.out_trade_no = getOutTradeNoRandom();
        bizContent.subject = subject;
        bizContent.user_id = uid;
        //设备编号
        bizContent.terminal_id = deviceNo;
        //支付授权码
        bizContent.auth_code = authCode;
        //设备返佣参数
        bizContent.terminal_params = JSONObject.parseObject(terminalParams);
        //商品
        bizContent.goods_detail = getGoodDetail(amount, goods).toString();

        //isv签约返佣协议账户的pid
        WithholdReq.ExtendParams extendParams = new WithholdReq.ExtendParams();
        extendParams.sys_service_provider_id = isvPid;
        bizContent.extend_params = extendParams;

        WithholdReq.BusinessParams businessParams = new WithholdReq.BusinessParams();
        businessParams.iot_pay_action = isFace ? IotPayAction.online_face_pay.name() : IotPayAction.bar_pay.name();
        bizContent.business_params = businessParams;

        //产品码-代扣
        bizContent.product_code = isWithhold ? ProductCode.GENERAL_WITHHOLDING_P.name() : ProductCode.FACE_TO_FACE_PAYMENT.name();
        //支付场景 220811新增
        bizContent.scene = isFace ? Scene.security_code.name() : Scene.bar_code.name();
        if (isFace) {
            if (authCode.startsWith("AAAA"))
                bizContent.scene = Scene.codec_double_offline_code.name();
            else
                bizContent.scene = Scene.security_code.name();
        } else
            bizContent.scene = Scene.bar_code.name();

        if (!TextUtils.isEmpty(senceNo))
            bizContent.sence_no = senceNo;

        LogUtil.i("getBizContent" +
                "\nisFace: " + isFace +
                "\nisWithhold: " + isWithhold +
                "\nauthCode: " + authCode +
                "\nscene: " + bizContent.scene +
                "\nsence_no: " + bizContent.sence_no +
                "\nterminalParams: " + bizContent.terminal_params);
        return bizContent;
    }

    public static QuerySignBizContentReq getQuerySignBizContent(String uid) {
        QuerySignBizContentReq bizContentReq = new QuerySignBizContentReq();
        bizContentReq.product_code = ProductCode.GENERAL_WITHHOLDING_P.name();
        bizContentReq.user_id = uid;
        return bizContentReq;
    }

}
